package com.alibaba.algo;

import com.alibaba.algo.GenerateKernel.ModuleType;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * The Class Main.
 */
public class Main {

    /**
     * The main method.
     *
     * @param args the arguments
     * @throws Exception the exception
     */
    public static void main(String args[]) throws Exception {

        String module = GCConfig.getGenerateModule();
        String tables = GCConfig.getGenerateTable();
        String tableClass = GCConfig.getGenerateTableClass();

        Map<String, Object> dataMap = new HashMap<>();
        setCommonVariables(dataMap, module, tableClass);

        if (StringUtils.isNotBlank(tables)) {
            for (String tableName : tables.split(",")) {
                for (String clazzModule : tableClass.split(",")) {
                    System.out.println(tableName);
                    GenerateKernel.generateToFile(dataMap, tableName, clazzModule);
                }
            }
        }

        if (StringUtils.isNotBlank(module)) {
            for (String moduleStr : module.split(",")) {
                GenerateKernel.generateToFile(dataMap, null, moduleStr);
            }
        }

        System.out.println("*************************generate class success");

    }

    /**
     * Sets the common variables.
     *
     * @param dataMap    the data map
     * @param module     the module
     * @param tableClass the table class
     */
    private static void setCommonVariables(Map<String, Object> dataMap, String module, String tableClass) {
        String path = "";
        if (StringUtils.isNotBlank(tableClass)) {
            for (String clazzModule : tableClass.split(",")) {
                path = GCConfig.BUSSI_PACKAGE + "."
                        + ModuleType.valueOf(clazzModule).getRelativePath().replace("/", ".");
                dataMap.put(ModuleType.valueOf(clazzModule).getVariable(), path);
            }
        }

        if (StringUtils.isNotBlank(module)) {
            for (String moduleStr : module.split(",")) {
                path = GCConfig.BUSSI_PACKAGE + "." + ModuleType.valueOf(moduleStr).getRelativePath().replace("/", ".");
                dataMap.put(ModuleType.valueOf(moduleStr).getVariable(), path);
            }
        }

        dataMap.put(VariablesConstant.MYBATIS_OPEN, "#{");
        dataMap.put(VariablesConstant.MYBATIS_CLOSE, "}");
        dataMap.put(VariablesConstant.BUSSI_PACKAGE, GCConfig.BUSSI_PACKAGE);
        dataMap.put(VariablesConstant.PRODUCT_CODE, GCConfig.PRODUCT_CODE);
        dataMap.put(VariablesConstant.PRODUCT_NAME, GCConfig.PRODUCT_NAME);
        dataMap.put(VariablesConstant.AUTHOR, GCConfig.AUTHOR);
        dataMap.put(VariablesConstant.VERSION, GCConfig.VERSION);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dataMap.put(VariablesConstant.CREAT_TIME, format.format(new Date()));
        dataMap.put("serialVersionUID", String.valueOf(1));

        dataMap.put(VariablesConstant.COMMON_PACK_PATH, GCConfig.COMMON_PACK_PATH);
    }
}
